﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace mForum.Core.Specification
{
    public class OrSpecification<TEntity> : CompositeSpecification<TEntity> where TEntity : class
    {
        private ISpecification<TEntity> _rightSideSpecification = null;
        private ISpecification<TEntity> _leftSideSpecification = null;

        public OrSpecification(ISpecification<TEntity> leftSide, ISpecification<TEntity> rightSide)
        {
            if (leftSide == (ISpecification<TEntity>)null)
                throw new ArgumentNullException("leftSide");

            if (rightSide == (ISpecification<TEntity>)null)
                throw new ArgumentNullException("rightSide");

            this._leftSideSpecification = leftSide;
            this._rightSideSpecification = rightSide;
        }

        public override ISpecification<TEntity> LeftSideSpecification
        {
            get { return _leftSideSpecification; }
        }

        public override ISpecification<TEntity> RightSideSpecification
        {
            get { return _rightSideSpecification; }
        }

        public override Expression<Func<TEntity, bool>> SatisfiedBy()
        {
            Expression<Func<TEntity, bool>> left = _leftSideSpecification.SatisfiedBy();
            Expression<Func<TEntity, bool>> right = _rightSideSpecification.SatisfiedBy();

            return left.Or(right);
        }
    }
}
